package com.wlb365.admin.modules.schedule.job;

import com.wlb365.admin.common.utils.SdkUtils;
import com.wlb365.admin.modules.constant.JobConstant;
import com.wlb365.admin.modules.schedule.utils.DateUtil;
import com.wlb365.sdk.client.SdkClient;
import com.wlb365.sdk.model.CommonRequest;
import com.wlb365.sdk.model.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 概述：账户定时对账
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2020/5/22
 */
@Slf4j
@Component
public class AcctVerifyJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 获得传入的参数
        String params = jobExecutionContext.getMergedJobDataMap().getString(JobConstant.JOB_MAP_KEY);
        log.info("acctVerifyJob is running star params={}, time:{}", params, new Date());

        //日期减一，下载前一天对账文件
        LocalDate localDate = LocalDate.now();
        LocalDate yesterday = localDate.plusDays(-1);
        String date = DateUtil.getDateStr(yesterday);
        //获取sdk客户端
        SdkClient sdkClient = SdkUtils.getClient();

        CommonRequest request = new CommonRequest("zbank.verify.acct");
        Map<String, Object> bizModel = new HashMap<>();
        bizModel.put("rcnclDt", date);

        request.setBizModel(bizModel);
        CommonResponse response = sdkClient.execute(request);
        if ("000000".equals(response.getStatus())) {
            log.info("定时对账成功");
        } else {
            log.info("定时对账失败");
        }

        log.info("acctVerifyJob is running end params={}, time:{}", params, new Date());
    }
}
